package mycoffee.order.adapter;

import java.util.List;

import mycoffee.order.R;
import mycoffee.order.model.Constants;
import mycoffee.order.model.Food;
import mycoffee.order.model.Order;
import mycoffee.order.model.OrdersDetail;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * @author Tuong
 * 
 */
public class OrderAdapter extends BaseAdapter {
	/** Interface to global information about an application environment. */
	protected Context mContext;
	/** used to instantiate layout XML file into its corresponding View objects. */
	private LayoutInflater infalInflater;
	private List<OrdersDetail> mOrdersDetails;
	private int mCurrentOrderDetailSelectedIndex = Constants.INVALID_VALUE;

	public OrderAdapter(Context context, List<OrdersDetail> objects) {
		super();
		mOrdersDetails = objects;
		mContext = context;
		infalInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		if (mOrdersDetails != null) {
			return mOrdersDetails.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (mOrdersDetails != null && mOrdersDetails.size() > position
				&& position >= 0) {
			return mOrdersDetails.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = infalInflater.inflate(R.layout.order_item, null);

			viewHolder = new ViewHolder();
			viewHolder.mTvFoodName = (TextView) convertView
					.findViewById(R.id.order_item_tv_food_name);
			viewHolder.mTvFoodAmount = (TextView) convertView
					.findViewById(R.id.order_item_tv_amount);
			viewHolder.mTvFoodPrice = (TextView) convertView
					.findViewById(R.id.order_item_tv_food_price);
			viewHolder.mTvFoodTotal = (TextView) convertView
					.findViewById(R.id.order_item_tv_food_total);
			viewHolder.mIvDelete = (LinearLayout) convertView
					.findViewById(R.id.order_item_iv_delete);

			viewHolder.mLlEdit = (LinearLayout) convertView
					.findViewById(R.id.order_item_ll_edit);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (position == mCurrentOrderDetailSelectedIndex) {
			viewHolder.mLlEdit.setVisibility(View.VISIBLE);
		} else {
			viewHolder.mLlEdit.setVisibility(View.GONE);
		}

		viewHolder.mIvDelete.setTag(position);
		// update data.
		OrdersDetail ordersDetail = mOrdersDetails.get(position);

		if (ordersDetail == null) {
			return null;
		}
		Food food = ordersDetail.getFood();
		if (food != null) {
			viewHolder.mTvFoodName.setText(food.getNameFollowCurrentLanguage());
			viewHolder.mTvFoodAmount.setText("" + ordersDetail.getAmount());
			viewHolder.mTvFoodPrice.setText("" + food.getPrice());
			viewHolder.mTvFoodTotal.setText("" + food.getPrice()
					* ordersDetail.getAmount());
		}

		return convertView;
	}

	/**
	 * 
	 * @param currentOrder
	 */
	public void setCurrentOrder(Order currentOrder) {
		if (mOrdersDetails == null || currentOrder == null) {
			return;
		}
		mOrdersDetails = currentOrder.getOrdersDetails();
		mCurrentOrderDetailSelectedIndex = Constants.INVALID_VALUE;
		notifyDataSetChanged();

	}

	public void setOrderDetailSelectedIndex(int index) {

		mCurrentOrderDetailSelectedIndex = index;
		notifyDataSetChanged();
	}

	class ViewHolder {
		private TextView mTvFoodName;
		private TextView mTvFoodAmount;
		private TextView mTvFoodPrice;
		private TextView mTvFoodTotal;
		private LinearLayout mIvDelete;
		private LinearLayout mLlEdit;
	}

}
